#!/usr/bin/perl
#
# Check for bad words

$|=1 ;
use utf8  ;

my $good_file = 'good.txt' ;
my $bad_file = 'bad.txt' ;

my $bad_re = ' ';
my %good ;
my %bad ;

## Loading bad words 
open FF, "< $bad_file"
	or die "Error while opening file: $bad_file" ;
while (<FF>){
	chomp ;
	$bad_re .= "|" . $_ ;
	$bad{$_} = 1 ;
}
close FF ;

## Load good words
open FF, "< $good_file"
	or die "Error while opening file: $good_file" ;
while(<FF>) {
	chomp ;
	$good{$_} = 1 ;
}
close FF ;

while (<>){
chomp ;
my $line = $_ ;
utf8::decode($line) ;
$line = lc($line) ;
$line =~ y/3046uetopahkxcbmy/зочбиеторанкхсвму/ ;
$line =~ s/j[1l]/л/g ;
$line =~ s/[^а-яА-ЯёЁ]/ /g;
$line =~ y/[А-ЯЁ]/[а-яё]/ ;
$line =~ s/[ъь]//g ;
$line =~ s/нн/н/g ;
$line =~ s/лл/л/g ;
$line =~ y/щйёэ/шиее/ ;
utf8::encode($line) ;

### First check
## Simple match
my @words = split /\s+/, $line ;
foreach my $word (@words) {
	if (length($word) >= 6) {
		if($word =~ /($bad_re)/) {
			if (!($good{$word})) {
				print "DETECT: $word: $line ($_)\n" ;
			}else{
				if($bad{$word}) {
					print "DETECT STRONG: $word: $line ($_)\n" ;
				}
			}
		}
	}
}
}
